﻿using System;

public class Mapper075 : MapperDefault
{
    internal int[] regs = new int[2];

    public override void init(NES nes)
    {
        base.init(nes);
    }

    public override void loadROM(ROM rom)
    {
        int num = rom.RomBankCount * 2;
        this.load8kRomBank(0, 0x8000);
        this.load8kRomBank(1, 0xa000);
        this.load8kRomBank(num - 2, 0xc000);
        this.load8kRomBank(num - 1, 0xe000);
        this.loadCHRROM();
        base.nes.Cpu.requestIrq(2);
    }

    public override void reset()
    {
        this.regs[0] = 0;
        this.regs[1] = 1;
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
        }
        else
        {
            int num = address & 0xf000;
            if (num <= 0xa000)
            {
                switch (num)
                {
                    case 0x8000:
                        this.load8kRomBank(value_Renamed, 0x8000);
                        return;

                    case 0x9000:
                        if ((value_Renamed & 1) != 0)
                        {
                            base.nes.Ppu.Mirroring = 1;
                        }
                        else
                        {
                            base.nes.Ppu.Mirroring = 0;
                        }
                        this.regs[0] = (this.regs[0] & 15) | ((value_Renamed & 2) << 3);
                        this.loadVromBank(this.regs[0], 0);
                        this.regs[1] = (this.regs[1] & 15) | ((value_Renamed & 4) << 2);
                        this.loadVromBank(this.regs[1], 0x1000);
                        return;
                }
                if (num == 0xa000)
                {
                    this.load8kRomBank(value_Renamed, 0xa000);
                }
            }
            else
            {
                switch (num)
                {
                    case 0xc000:
                        this.load8kRomBank(value_Renamed, 0xc000);
                        return;

                    case 0xe000:
                        this.regs[0] = (this.regs[0] & 0x10) | (value_Renamed & 15);
                        this.loadVromBank(this.regs[0], 0);
                        return;

                    case 0xf000:
                        this.regs[1] = (this.regs[1] & 0x10) | (value_Renamed & 15);
                        this.loadVromBank(this.regs[1], 0x1000);
                        return;
                }
            }
        }
    }
}

